package com.ruoyi.web.controller.social.examine;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ExaminePlan;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ExaminePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ruohao
 * @Date: 2023/9/3
 **/
@RestController
@RequestMapping("/social/examine/plan")
public class ExaminePlanController extends BaseController {
    @Autowired
    private ExaminePlanService examinePlanService;

    @Log(title = "查询考核方案", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(ExaminePlan examinePlan) {
        startPage();
        List<ExaminePlan> list = examinePlanService.selectList(examinePlan);
        return getDataTable(list);
    }


    @Log(title = "查询考核方案All", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(ExaminePlan examinePlan) {
        List<ExaminePlan> list = examinePlanService.selectList(examinePlan);
        list = list.stream().filter(e -> "启用".equals(e.getStatus())).collect(Collectors.toList());
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(examinePlanService.selectById(id));
    }

    @Log(title = "新增考核方案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@Validated @RequestBody ExaminePlan examinePlan) {

        //查询该类型已启用方案
        if (StringUtils.isNotBlank(examinePlan.getType())) {
            ExaminePlan query = new ExaminePlan();
            query.setStatus("启用");
            query.setType(examinePlan.getType());
            List<ExaminePlan> list = examinePlanService.selectList(query);
            if (!CollectionUtils.isEmpty(list)) {
                throw new BaseException("该类型已有启用的方案，不能同时存在多条相同类型启用的方案" + query.getType());
            }
        }

        examinePlan.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = examinePlanService.create(examinePlan);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改考核方案", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@Validated @RequestBody ExaminePlan examinePlan) {

        ExaminePlan old = examinePlanService.selectById(examinePlan.getId());
        if (old != null) {
            if (StringUtils.isNotBlank(examinePlan.getStatus()) && StringUtils.isNotBlank(examinePlan.getType())) {
                if (examinePlan.getType().equals("启用")) {
                    ExaminePlan query = new ExaminePlan();
                    query.setStatus("启用");
                    query.setType(examinePlan.getType());
                    List<ExaminePlan> list = examinePlanService.selectList(query);
                    if (!CollectionUtils.isEmpty(list)) {
                        throw new BaseException("该类型已有启用的方案，不能同时存在多条相同类型启用的方案" + query.getType());
                    }
                }
            }
        }

        examinePlan.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = examinePlanService.update(examinePlan);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "删除考核方案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long id) {
        ExaminePlan examinePlan = examinePlanService.selectById(id);
        if (examinePlan != null) {
            if (StringUtils.isNotBlank(examinePlan.getStatus())) {
                if (examinePlan.getStatus().equals("启用")) {
                    throw new BaseException("已启用的方案不能删除");
                }
            }
        }
        return toAjax(examinePlanService.deleteById(id));
    }


}
